package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.domain.Permission;
import com.itheima.domain.Role;
import com.itheima.service.RoleService;
import com.itheima.util.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class RoleServiceImpl implements RoleService {



    @Override
    public List<Role> findAll() {
        //获取Dao对象
        SqlSession sqlSession = MyBatisUtils.openSession();
        RoleDao roleDao = sqlSession.getMapper(RoleDao.class);
        //查询所有
        List<Role> list = roleDao.findAll();
        //释放资源
        sqlSession.close();
        return list;
    }

    /**
     * 保存
     * @param role
     */
    @Override
    public void save(Role role) {
        //获取Dao对象
        SqlSession sqlSession = MyBatisUtils.openSession();
        RoleDao roleDao = sqlSession.getMapper(RoleDao.class);
        //添加
        roleDao.save(role);
        //获取用户提交的权限所对应的id
        List<Integer> permissionIds = role.getPermissionIds();
        if (permissionIds != null || permissionIds.size() > 0) {
            for (Integer permissionId : permissionIds) {
                //将角色信息和权限写入到中表中
                roleDao.saveRoleAndPerssion(role.getId(), permissionId);

            }

        }
        //提交事务
        sqlSession.commit();
        //释放资源
        sqlSession.close();
    }

    @Override
    public PageInfo<Role> findByPage(Integer pageNum, Integer pageSize) {
        //获取Dao对象
        SqlSession sqlSession = MyBatisUtils.openSession();
        RoleDao roleDao = sqlSession.getMapper(RoleDao.class);
        //1. 查询数据
        PageHelper.startPage(pageNum,pageSize);

        //调用dao查询数据
        List<Role> list = roleDao.findList();

        //5. 释放资源
        sqlSession.close();
        return new PageInfo<>(list);
    }

    @Override
    public Role findById(Integer id) {
        //获取Dao对象
        SqlSession sqlSession = MyBatisUtils.openSession();
        RoleDao roleDao = sqlSession.getMapper(RoleDao.class);
        //查询
        Role role = roleDao.findById(id);
        //释放资源
        sqlSession.close();
        return role;
    }

    /**
     * 修改
     * @param role
     */
    @Override
    public void update(Role role) {
        //获取Dao对象
        SqlSession sqlSession = MyBatisUtils.openSession();
        RoleDao roleDao = sqlSession.getMapper(RoleDao.class);
        //修改
        roleDao.update(role);
        //先将之间的角色和权限的依赖删除，
        roleDao.deleteRoleAndPer(role.getId());
        //然后把新提交的角色和权限绑定上
        List<Integer> permissionIds = role.getPermissionIds();
        if (permissionIds != null || permissionIds.size() > 0) {
            for (Integer permissionId : permissionIds) {
                roleDao.saveRoleAndPerssion(role.getId(),permissionId);
            }

        }
        //提交事务
        sqlSession.commit();
        //释放资源
        sqlSession.close();
    }

    @Override
    public void delete(Integer id) {
        //获取dao对象
        SqlSession sqlSession = MyBatisUtils.openSession();
        RoleDao roleDao = sqlSession.getMapper(RoleDao.class);
        //先将中间表对应的数据删除
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        mapper.deleteUserRoleRelation(id);

        roleDao.deleteRoleAndPer(id);
        roleDao.delete(id);
        //提交事务
        sqlSession.commit();
        //释放资源
        sqlSession.close();
    }
}
